package com.payment.system.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.payment.common.support.Convert;
import com.payment.common.utils.DateUtils;
import com.payment.common.utils.StringUtils;
import com.payment.system.domain.Acq;
import com.payment.system.domain.AcqBind;
import com.payment.system.domain.PlanNew;
import com.payment.system.domain.Remind;
import com.payment.system.mapper.AcqBindMapper;
import com.payment.system.mapper.AcqMapper;
import com.payment.system.mapper.MerchantIncreaseLimitMapper;
import com.payment.system.mapper.PlanNewMapper;
import com.payment.system.mapper.RemindMapper;
import com.payment.system.service.IRemindService;
import com.payment.system.vo.CreditCardVo;

/**
 * 智能提现备注 服务层实现
 * 
 * @author payment
 * @date 2019-12-16
 */
@Service
public class RemindServiceImpl implements IRemindService 
{
	@Autowired
	private RemindMapper remindMapper;
	@Autowired
	private MerchantIncreaseLimitMapper limitMapper;
	@Autowired
	private PlanNewMapper planNewMapper;
	@Autowired
	private AcqMapper acqMapper;
	@Autowired
	private AcqBindMapper acqBindMapper;

	/**
     * 查询智能提现备注信息
     * 
     * @param id 智能提现备注ID
     * @return 智能提现备注信息
     */
    @Override
	public Remind selectRemindById(String id)
	{
	    return remindMapper.selectRemindById(id);
	}
	
	/**
     * 查询智能提现备注列表
     * 
     * @param remind 智能提现备注信息
     * @return 智能提现备注集合
     */
	@Override
	public List<Remind> selectRemindList(Remind remind)
	{
	    return remindMapper.selectRemindList(remind);
	}
	
    /**
     * 新增智能提现备注
     * 
     * @param remind 智能提现备注信息
     * @return 结果
     */
	@Override
	public int insertRemind(Remind remind)
	{
	    return remindMapper.insertRemind(remind);
	}
	
	/**
     * 修改智能提现备注
     * 
     * @param remind 智能提现备注信息
     * @return 结果
     */
	@Override
	public int updateRemind(Remind remind)
	{
	    return remindMapper.updateRemind(remind);
	}

	/**
     * 删除智能提现备注对象
     * 
     * @param ids 需要删除的数据ID
     * @return 结果
     */
	@Override
	public int deleteRemindByIds(String ids)
	{
		return remindMapper.deleteRemindByIds(Convert.toStrArray(ids));
	}

	@Override
	public List<Remind> selectRemindListByType(Integer type,String merchantId) {
		// TODO Auto-generated method stub
		List<Remind> l = remindMapper.selectRemindListByType(type,merchantId);
		List<Acq> acqList = acqMapper.selectAcqListByApp("");//获取所有通道
		List<Remind> li = new ArrayList<Remind>();
		for (Remind remind : l) {
			Calendar calendar = Calendar.getInstance();
			int day = calendar.get(Calendar.DAY_OF_MONTH);  // 获取今天是几号
			int repaymentDay = Integer.parseInt(remind.getRepaymentDay()); // 还款日
			if (type == 0) {
				int billDay = Integer.parseInt(remind.getBillDay()); //账单日
				// 账9  还23
				if (billDay>repaymentDay) {
					//账单日大于还款日的 已还款日为基准判断
					if (day<=repaymentDay) {
						remind.setContext("本期账单已出，距离还款日还有"+(repaymentDay-day)+"天");
					} else if (day<billDay) {
						remind.setContext("距本期账单出账还有"+(billDay-day)+"天");
					} else if (day==billDay) {
						remind.setContext("今日出账请注意更新账单信息");
						remind.setPaymentMoney(BigDecimal.ZERO);
						remind.setIsPayment(0);
						remindMapper.updateRemind(remind);
					} else {
						//还款日延续到下个月   账23   还9
						Calendar repaymentCalendar = Calendar.getInstance();
						repaymentCalendar.add(Calendar.MONTH, 1);
						repaymentCalendar.set(Calendar.DAY_OF_MONTH, repaymentDay);
						int days =	DateUtils.compareDateDayTwo(repaymentCalendar.getTime());
						remind.setContext("本期账单已出，距离还款日还有"+(days)+"天");
						if (remind.getIsPayment().equals(1)){
							remind.setContext("本期账单已还完");
						}
					}
				} else {
					//账单日小于还款日的 已账单日为基准判断
					if (day<billDay) {
						remind.setContext("距本期账单出账还有"+(billDay-day)+"天");
					} else if (day==billDay) {
						remind.setContext("今日出账请注意更新账单信息");
						remind.setPaymentMoney(BigDecimal.ZERO);
						remind.setIsPayment(0);
						remindMapper.updateRemind(remind);
					} else if (day<=repaymentDay) {
						if (remind.getIsPayment().equals(1)){
							remind.setContext("本期账单已还完");
						}
						remind.setContext("本期账单已出，距离还款日还有"+(repaymentDay-day)+"天");
					}  else {
						//还款日延续到下个月   账23   还9
						Calendar repaymentCalendar = Calendar.getInstance();
						repaymentCalendar.add(Calendar.MONTH, 1);
						repaymentCalendar.set(Calendar.DAY_OF_MONTH, repaymentDay);
						int days =	DateUtils.compareDateDayTwo(repaymentCalendar.getTime());
						remind.setContext("距本期账单出账还有"+(days)+"天");
					}
				}
				CreditCardVo limit = limitMapper.selectCardVoById(remind.getCardId());
				remind.setLimit(limit);
				remind.setRemarks(limit.getBankLogoImg());
				List<PlanNew> planList = planNewMapper.selectBadDebt(limit.getBankAccount());
				if (planList != null && planList.size() > 0) {
					remind.setPlanId(planList.get(0).getId());
					remind.setPlan(planList.get(0));
					remind.setRepaymentMoney(planList.get(0).getPlanAmt());
					if (planList.get(0).getStatus().equals(0)) {
						remind.setContext("本期账单已设置计划：待执行");
					} else if (planList.get(0).getStatus().equals(1)) {
						remind.setContext("本期账单已设置计划：执行中");
					} else if (planList.get(0).getStatus().equals(2)) {
						remind.setContext("本期账单已设置计划：失败");
					} else if (planList.get(0).getStatus().equals(2)) {
						remind.setContext("本期账单已设置计划：暂停");
					}
				} else {
					remind.setPlanId("0");
					remind.setPlan(new PlanNew());
				}
				List<Acq> acqListRm = acqList; 
				List<AcqBind> bindList = acqBindMapper.selectByCardId(limit.getId());
				String acqIds = "";
		        for (AcqBind acqBind : bindList) {
		            acqIds += acqBind.getAcqId() + ",";
		        }
		        if(StringUtils.isNotEmpty(acqIds)){
					acqIds = acqIds.substring(0, acqIds.length() - 1);
				}
				List<Acq> romveList=new ArrayList<Acq>();
				for (Acq acq : acqListRm) {
		    		if(!StringUtils.isEmpty(acq.getSupportBankcode())){
		    			String [] support = acq.getSupportBankcode().split(",");
		    			if(support != null && !ArrayUtils.contains(support, limit.getBankCode())){
		    				romveList.add(acq);
		    			}
		    		}
		    		if(!StringUtils.isEmpty(acq.getNoSupportBankcode())){
		    			String [] noSuppor = acq.getNoSupportBankcode().split(",");
		    			if(noSuppor != null && ArrayUtils.contains(noSuppor, limit.getBankCode())){
		    				romveList.add(acq);
		    			}
		    		}
		    		if (acqIds.indexOf(acq.getId())>-1){
		    			romveList.add(acq);
		    		}
				}
				acqListRm.removeAll(romveList);
		    	remind.setAcqNum(acqListRm.size());
			} else {
				/*if (day==billDay) {
					remind.setContext("今日出账请注意更新账单信息");
					remind.setPaymentMoney(BigDecimal.ZERO);
					remind.setIsPayment(0);
					remindMapper.updateRemind(remind);
				}*/
				if (repaymentDay>day) {
					remind.setContext("本期账单已出，距离还款日还有"+(repaymentDay-day)+"天");
				} else if(repaymentDay==day) {
					remind.setContext("今天是还款日，请注意是否处理");
				} else {
					Calendar calendar1 = Calendar.getInstance();
					calendar1.set(Calendar.DATE, Integer.parseInt(remind.getRepaymentDay()));
					calendar1.add(Calendar.MONTH, 1);
					long timec = calendar1.getTime().getTime()-DateUtils.getNowDate().getTime();
					int dayc = (int) (timec /1000/60/60/24);
					remind.setContext("本期账单已出，距离还款日还有"+dayc+"天");
				}
			}
			li.add(remind);
		}
		return li;
	}

	@Override
	public List<Remind> selctRemindByCardId(String cardId) {
		// TODO Auto-generated method stub
		return remindMapper.selctRemindByCardId(cardId);
	}
}
